package com.example.zhaoyulu.yunzhiapp.fragement;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.telecom.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.network.getRetrofitService;
import com.example.network.javaBeans.Book;
import com.example.network.javaBeans.yzjyinfo;
import com.example.zhaoyulu.yunzhiapp.AdapterPackages.ziXunAdapter;
import com.example.zhaoyulu.yunzhiapp.LoginActivity;
import com.example.zhaoyulu.yunzhiapp.MainActivity;
import com.example.zhaoyulu.yunzhiapp.R;
import com.example.zhaoyulu.yunzhiapp.watchVideoActivity;
import com.example.zhaoyulu.yunzhiapp.webActivity;
import com.example.zhaoyulu.yunzhiapp.zylUtils.GlideRoundTransform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zhaoyulu on 5/9/18.
 */

public class zixunFragment extends Fragment {

//    public void initView(ArrayList<yzjyinfo> yzjyinfos){
//        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
//        mziXunAdapter = new ziXunAdapter(yzjyinfos,getActivity());
//        recyclerView.setLayoutManager(mLayoutManager);
//        recyclerView.setAdapter(mziXunAdapter);
//
//    }
//    private RecyclerView recyclerView;
//    private ziXunAdapter mziXunAdapter;
//    private RecyclerView.LayoutManager mLayoutManager;


    HashMap<String,yzjyinfo> dataMap=new HashMap<>();
    private void initData(List<yzjyinfo> yzjyinfos){
        for(int i=0;i<yzjyinfos.size();i++){
            dataMap.put(yzjyinfos.get(i).getCategories(),yzjyinfos.get(i));
        }
    }
    private void initView(View view){
        //video
        ImageView mainzixun_im1_1=view.findViewById(R.id.mainzixun_im1_1);
        TextView mainzixun_im1_tv1=view.findViewById(R.id.mainzixun_im1_tv1);
        initChildViewVideo(mainzixun_im1_1,mainzixun_im1_tv1,"11");

        ImageView mainzixun_im1_2=view.findViewById(R.id.mainzixun_im1_2);
        TextView mainzixun_im1_tv2=view.findViewById(R.id.mainzixun_im1_tv2);
        initChildViewVideo(mainzixun_im1_2,mainzixun_im1_tv2,"12");

        ImageView mainzixun_im1_3=view.findViewById(R.id.mainzixun_im1_3);
        TextView mainzixun_im1_tv3=view.findViewById(R.id.mainzixun_im1_tv3);
        initChildViewVideo(mainzixun_im1_3,mainzixun_im1_tv3,"13");
        //teacher
        ImageView mainzixun_im2_1=view.findViewById(R.id.mainzixun_im2_1);
        TextView mainzixun_im2_tv1=view.findViewById(R.id.mainzixun_im2_tv1);
        initChildViewWeb(mainzixun_im2_1,mainzixun_im2_tv1,"21");

        ImageView mainzixun_im2_2=view.findViewById(R.id.mainzixun_im2_2);
        TextView mainzixun_im2_tv2=view.findViewById(R.id.mainzixun_im2_tv2);
        initChildViewWeb(mainzixun_im2_2,mainzixun_im2_tv2,"22");

        ImageView mainzixun_im2_3=view.findViewById(R.id.mainzixun_im2_3);
        TextView mainzixun_im2_tv3=view.findViewById(R.id.mainzixun_im2_tv3);
        initChildViewWeb(mainzixun_im2_3,mainzixun_im2_tv3,"23");

        ImageView mainzixun_im2_4=view.findViewById(R.id.mainzixun_im2_4);
        TextView mainzixun_im2_tv4=view.findViewById(R.id.mainzixun_im2_tv4);
        initChildViewWeb(mainzixun_im2_4,mainzixun_im2_tv4,"24");

        ImageView mainzixun_im2_5=view.findViewById(R.id.mainzixun_im2_5);
        TextView mainzixun_im2_tv5=view.findViewById(R.id.mainzixun_im2_tv5);
        initChildViewWeb(mainzixun_im2_5,mainzixun_im2_tv5,"25");

        //bigPict
        ImageView mainzixun_iv3=view.findViewById(R.id.mainzixun_iv3);
        TextView mainzixun_iv3_iv1=view.findViewById(R.id.mainzixun_iv3_iv1);
        initChildViewVideo(mainzixun_iv3,mainzixun_iv3_iv1,"31");

        //childPict1
        ImageView mainzixun_iv4_2_tv1=view.findViewById(R.id.mainzixun_iv4_2_tv1);
        TextView mainzixun_iv4_2_tv1_tv1=view.findViewById(R.id.mainzixun_iv4_2_tv1_tv1);
        initChildViewVideo(mainzixun_iv4_2_tv1,mainzixun_iv4_2_tv1_tv1,"41");

        ImageView mainzixun_iv4_1_tv1=view.findViewById(R.id.mainzixun_iv4_1_tv1);
        TextView mainzixun_iv4_1_tv1_tv1=view.findViewById(R.id.mainzixun_iv4_1_tv1_tv1);
        initChildViewVideo(mainzixun_iv4_1_tv1,mainzixun_iv4_1_tv1_tv1,"42");

        ImageView mainzixun_iv4_3_tv1=view.findViewById(R.id.mainzixun_iv4_3_tv1);
        TextView mainzixun_iv4_3_tv1_tv1=view.findViewById(R.id.mainzixun_iv4_3_tv1_tv1);
        initChildViewVideo(mainzixun_iv4_3_tv1,mainzixun_iv4_3_tv1_tv1,"43");

        //childPict2
        ImageView mainzixun_iv5_1_tv1=view.findViewById(R.id.mainzixun_iv5_1_tv1);
        TextView mainzixun_iv5_1_tv1_tv1=view.findViewById(R.id.mainzixun_iv5_1_tv1_tv1);
        initChildViewVideo(mainzixun_iv5_1_tv1,mainzixun_iv5_1_tv1_tv1,"51");

        ImageView mainzixun_iv9_2_tv1=view.findViewById(R.id.mainzixun_iv9_2_tv1);
        TextView mainzixun_iv9_2_tv1_tv1=view.findViewById(R.id.mainzixun_iv9_2_tv1_tv1);
        initChildViewVideo(mainzixun_iv9_2_tv1,mainzixun_iv9_2_tv1_tv1,"52");

        //childPict3

        ImageView mainzixun_iv6_2_tv1=view.findViewById(R.id.mainzixun_iv6_2_tv1);
        TextView mainzixun_iv6_2_tv1_tv1=view.findViewById(R.id.mainzixun_iv6_2_tv1_tv1);
        initChildViewVideo(mainzixun_iv6_2_tv1,mainzixun_iv6_2_tv1_tv1,"61");

        //childPict4
        ImageView mainzixun_iv6_1_tv1=view.findViewById(R.id.mainzixun_iv6_1_tv1);
        TextView mainzixun_iv6_1_tv1_tv1=view.findViewById(R.id.mainzixun_iv6_1_tv1_tv1);
        initChildViewVideo(mainzixun_iv6_1_tv1,mainzixun_iv6_1_tv1_tv1,"81");

        ImageView mainzixun_iv5_2_tv1=view.findViewById(R.id.mainzixun_iv5_2_tv1);
        TextView mainzixun_iv5_2_tv1_tv1=view.findViewById(R.id.mainzixun_iv5_2_tv1_tv1);
        initChildViewVideo(mainzixun_iv5_2_tv1,mainzixun_iv5_2_tv1_tv1,"82");

        //childPict5

        ImageView mainzixun_iv7_2_tv1=view.findViewById(R.id.mainzixun_iv7_2_tv1);
        TextView mainzixun_iv7_2_tv1_tv1=view.findViewById(R.id.mainzixun_iv7_2_tv1_tv1);
        initChildViewVideo(mainzixun_iv7_2_tv1,mainzixun_iv7_2_tv1_tv1,"71");

        ImageView mainzixun_iv7_1_tv1=view.findViewById(R.id.mainzixun_iv7_1_tv1);
        TextView mainzixun_iv7_1_tv1_tv1=view.findViewById(R.id.mainzixun_iv7_1_tv1_tv1);
        initChildViewVideo(mainzixun_iv7_1_tv1,mainzixun_iv7_1_tv1_tv1,"72");

        ImageView mainzixun_iv7_3_tv1=view.findViewById(R.id.mainzixun_iv7_3_tv1);
        TextView mainzixun_iv7_3_tv1_tv1=view.findViewById(R.id.mainzixun_iv7_3_tv1_tv1);
        initChildViewVideo(mainzixun_iv7_3_tv1,mainzixun_iv7_3_tv1_tv1,"73");



    }

    private void initChildViewWeb(ImageView imageView,TextView textView,final String key){
        Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(imageView);
        textView.setText(dataMap.get(key).getDescription());
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),webActivity.class);
                //用Bundle携带数据
                Bundle bundle=new Bundle();
                //传递name参数为tinyphp
                bundle.putString("url",dataMap.get(key).getUrl());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initChildViewVideo(ImageView imageView,TextView textView,final String key){
        Glide.with(getActivity()).load(dataMap.get(key).getPic()).centerCrop().transform(new GlideRoundTransform(getActivity())).into(imageView);
        textView.setText(dataMap.get(key).getDescription());
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),watchVideoActivity.class);
                //用Bundle携带数据
                Bundle bundle=new Bundle();
                //传递name参数为tinyphp
                bundle.putString("url",dataMap.get(key).getUrl());
                bundle.putString("id",dataMap.get(key).getID());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //引用创建好的xml布局
        final View view = inflater.inflate(R.layout.mainzixunfragment,container,false);
//        recyclerView=view.findViewById(R.id.zixunRecyclerView);
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        getRetrofitService.getService().getyzjyinfo().enqueue(new Callback<List<yzjyinfo>>() {
//            @Override
//            public void onResponse(retrofit2.Call<List<yzjyinfo>> call, Response<List<yzjyinfo>> response) {
//                if(response.isSuccessful()){
//                    initView((ArrayList<yzjyinfo>) response.body());
//                }
//
//            }
//
//            @Override
//            public void onFailure(retrofit2.Call<List<yzjyinfo>> call, Throwable t) {
//                    int j=0;
//            }
//        });
        getRetrofitService.getService().getyzjyinfo().enqueue(new Callback<List<yzjyinfo>>() {
            @Override
            public void onResponse(retrofit2.Call<List<yzjyinfo>> call, Response<List<yzjyinfo>> response) {
                int i=0;
                initData(response.body());
                initView(view);
            }

            @Override
            public void onFailure(retrofit2.Call<List<yzjyinfo>> call, Throwable t) {

            }
        });





        return view;
    }
}
